class SceneUI extends DateTimerUI {
    private evts: any;
    private isEvtChange: boolean;
    private evtNow: string;
    private sceneConf: any;
    private planMap: Array<any>;

    protected createView() {
        this.isEvtChange = false;

        this.sceneConf = RES.getRes("scene_json");
        this.planMap = this.createPlanMap(this.sceneConf.plan);

        this.addEventListener(egret.Event.ADDED_TO_STAGE, () => {
            this.createEvts(this.sceneConf.events);
        }, this);
    }

    protected timeUpdate(timer: DateTimer) {
        var evtWill = this.getEvtNameInPlan(timer.hour, timer.minute);
        if (!evtWill || evtWill == this.evtNow) return;

        this.changeEvt(evtWill);
    }    



    private createEvts(evt_names: string) {
        var names: Array<string> = evt_names.split(",");
        var evt: egret.Bitmap;

        this.evts = {};

        names.forEach((name: string) => {
            evt = this.createBitmapByName("imglist." + name);
            evt.anchorX = 0.5;
            evt.alpha = 0.9;
            evt.x = this.stage.stageWidth / 2;
            evt.y = this.stage.stageHeight * 2/ 3;
            evt.width = 0;
            this.addChild(evt);

            this.evts[name] = evt;
        })
    }

    private createPlanMap(plan: Array<any>):Array<any>{
        var planMap: Array<any> = [];
        var tmp: Array<string>;

        plan.forEach((item)=>{
          tmp = item[0].match(/\d+/g);
          planMap.push([
              this.timeToNum(tmp[0], tmp[1]),
              this.timeToNum(tmp[2], tmp[3]),
              item[1]
          ]);
        })

        return planMap;
    }

    private timeToNum(hour:string, minute:string):number{
        return Number(hour) + Number(minute) / 60;
    }

    private getEvtNameInPlan(hour:number, minute:number):string {
        var val = hour + minute / 60;

        var index = this.binarySearch(this.planMap, 0, this.planMap.length - 1, (plan: Array<any>) => {
            if(plan[0] <= val && val < plan[1]){
                return 0;
            }else if(val < plan[0]){
                return -1;
            }else{
                return 1;
            }
        });

        if(index >= 0){
            return this.planMap[index][2];
        }

    }

    private binarySearch(arr: Array<any>, start: number, end: number, fnCheck: Function): number {
      var index = Math.floor((start + end) / 2);
      var res = fnCheck(arr[index]);

      if(res == 0){
          return index;
      }

      if(start == end){
          return -1;
      }

      if(res > 0){
          return this.binarySearch(arr, index + 1, end, fnCheck);
      }else{
          return this.binarySearch(arr, start, index - 1, fnCheck);
      }
    }

    private changeEvt(evtWill:string) {
        if (this.isEvtChange) return;
        this.isEvtChange = true;

        var done: Function = () => {
            this.isEvtChange = false;
            this.evtNow = evtWill;
        };

        if (this.evtNow) {
            this.hideEvt(this.evtNow, () => {
                this.showEvt(evtWill, () => {
                    done();
                });
            })
        }else{
          this.showEvt(evtWill, () => {
              done();
          });          
        }
    }

    private hideEvt(name: string, fnBack: Function) {
        egret.Tween.get(this.evts[name]).to({
            width: 0
        }, 300).call(fnBack);
    }

    private showEvt(name: string, fnBack: Function) {
        var evt: egret.Bitmap = this.evts[name];
        egret.Tween.get(evt).to({
            width: evt.texture.textureWidth
        }, 300).call(fnBack);
    }
}